﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;

namespace CECB.Matrix
{
    /// <summary>
    /// Interaction logic for PendingMatrixValues_Window.xaml
    /// </summary>
    public partial class PendingMatrixValues_Window : Window
    {
        public PendingMatrixValues_Window()
        {
            InitializeComponent();
            dataContext = new DSSEntities();

            refreshData();
             
        }

        DSSEntities dataContext;
        private Boolean isSimpleMatrix;

        private void refreshData() { 
        
            ProjectAutocompleteTextBox.DataContext = dataContext.Projects;
            
             
            listBoxMembersValuesLeo.ItemsSource = null;
            listBoxMembersValuesLeo.Items.Clear();
            
            
            listBoxMembersValuesSim.ItemsSource = null;
            listBoxMembersValuesSim.Items.Clear();
            
            LableCurrentImpValues.Content = "";
            LableCurrentMagValues.Content = "";
           
            refreshListBox(isSimpleMatrix);
            listBoxProjectsLeo.ItemsSource = (from c in dataContext.LeopoldMatrixValuesExtra
                                              select c.Projects).Distinct();
            listBoxProjectsSim.ItemsSource = (from c in dataContext.SimpleMatrixValuesExtra
                                              select c.Projects).Distinct();

        }


        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        Projects proj;


        private void refreshListBox(Boolean isSimple) {
            if (!isSimple)
            {
                List<LeopoldMatrixValuesExtra> tempList = listBoxAllActImp.Items.Cast<LeopoldMatrixValuesExtra>().ToList();
                int index = listBoxAllActImp.SelectedIndex;
                listBoxAllActImp.ItemsSource = null;
                if (index > -1)
                    tempList.RemoveAt(index);
                listBoxAllActImp.ItemsSource = tempList;
            }
            else {


                List<SimpleMatrixValuesExtra> tempList = listBoxAllActImp.Items.Cast<SimpleMatrixValuesExtra>().ToList();
                int index = listBoxAllActImp.SelectedIndex;
                listBoxAllActImp.ItemsSource = null;
                if (index > -1)
                    tempList.RemoveAt(index);
                listBoxAllActImp.ItemsSource = tempList;
            }
        
        }

        private void filterActivitiesImpacts(String proj , Boolean isSimple)
        {
            if (!isSimple)
            {
                List<LeopoldMatrixValuesExtra> x = (from c in dataContext.LeopoldMatrixValuesExtra
                                                   where c.proj_reference == proj

                                                   select c).ToList();
              
                for (int j = 0; j < x.Count; j++)
                {

                    for (int p = 1; p < x.Count; p++)
                    {
                        if (j < p)
                        {
                            if (((LeopoldMatrixValuesExtra)x[j]).actv_reference == ((LeopoldMatrixValuesExtra)x[p]).actv_reference)
                            {
                                x.RemoveAt(j);
                            }
                        }
                    }

                }
                listBoxAllActImp.ItemsSource = x;

            }
            else {


                List<SimpleMatrixValuesExtra> x = (from c in dataContext.SimpleMatrixValuesExtra
                                                          where c.proj_reference == proj

                                                          select c).ToList();
                int i = x.Count;
                for (int j = 0; j < x.Count; j++)
                {

                    for (int p = 1; p < x.Count; p++)
                    {
                        if (j < p)
                        {
                            if (((SimpleMatrixValuesExtra)x[j]).actv_reference == ((SimpleMatrixValuesExtra)x[p]).actv_reference)
                            {
                                x.RemoveAt(j);
                            }
                        }
                    }

                }
                listBoxAllActImp.ItemsSource = x;

            }
        }

 

        private void listBoxAllActImp_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (((ListBox)sender).SelectedItem != null)
            {

                if (!isSimpleMatrix)
                {
                    LeopoldMatrixValuesExtra selectedItem = (LeopoldMatrixValuesExtra)listBoxAllActImp.SelectedItem;
                    
                    listBoxMembersValuesSim.Visibility = System.Windows.Visibility.Collapsed;
                    
                    listBoxMembersValuesLeo.Visibility = System.Windows.Visibility.Visible;
                    
                    listBoxMembersValuesLeo.ItemsSource = from c in dataContext.LeopoldMatrixValuesExtra
                                                       where c.proj_reference == selectedItem.proj_reference
                                                       && c.impt_reference == selectedItem.impt_reference
                                                       && c.actv_reference == selectedItem.actv_reference

                                                       select c;

                    try
                    {
                        LeopoldMatrixValues values = (from c in dataContext.LeopoldMatrixValues
                                                   where c.proj_reference == selectedItem.proj_reference
                                                   && c.impt_reference == selectedItem.impt_reference
                                                   && c.actv_reference == selectedItem.actv_reference
                                                   select c).First();
                        if (values != null)
                        {
                            LableCurrentMagValues.Content = values.mtrx_magnitude;
                            LableCurrentImpValues.Content = values.mtrx_importance;
                        }
                        else
                        {
                            LableCurrentMagValues.Content = "No values assigned";
                            LableCurrentImpValues.Content = "No values assigned";
                        }
                    }
                    catch (Exception)
                    {
                        LableCurrentMagValues.Content = "No values assigned";
                        LableCurrentImpValues.Content = "No values assigned";
                    }
                }
                else {

                    SimpleMatrixValuesExtra selectedItem = (SimpleMatrixValuesExtra)listBoxAllActImp.SelectedItem;
                    
                    listBoxMembersValuesLeo.Visibility = System.Windows.Visibility.Collapsed;
                    
                    listBoxMembersValuesSim.Visibility = System.Windows.Visibility.Visible;
                    
                    listBoxMembersValuesSim.ItemsSource = from c in dataContext.SimpleMatrixValuesExtra
                                                       where c.proj_reference == selectedItem.proj_reference
                                                       && c.impt_reference == selectedItem.impt_reference
                                                       && c.actv_reference == selectedItem.actv_reference

                                                       select c;

                    try
                    {
                        SimpleMatrixValues val = (from c in dataContext.SimpleMatrixValues
                                                   where c.proj_reference == selectedItem.proj_reference
                                                   && c.impt_reference == selectedItem.impt_reference
                                                   && c.actv_reference == selectedItem.actv_reference
                                                   select c).First();
                        if (val != null)
                        {
                            LableCurrentMagValues.Content = val.mtrx_value;
                           // LableCurrentImpValues.Content = val.mtrx_importance;
                        }
                        else
                        {

                            LableCurrentMagValues.Content = "No values assigned";
                            LableCurrentImpValues.Content = "No values assigned";
                        }
                    }
                    catch (Exception)
                    {

                        LableCurrentMagValues.Content = "No values assigned";
                        LableCurrentImpValues.Content = "No values assigned";
                    }
                }
            }
        }

        private void saveLeopoldValues() {

            LeopoldMatrixValuesExtra leoExtra = (LeopoldMatrixValuesExtra)listBoxMembersValuesLeo.SelectedItem;

            try
            {
                if (new cecbClasses.Data_Adapters.Func_FillMatrix().InsertLeopoldValues(leoExtra.impt_reference, leoExtra.actv_reference, proj.proj_reference, leoExtra.mtrx_importance.ToString(), leoExtra.mtrx_magnitude.ToString() ))
                {
                    MessageBox.Show("Value added to matrix!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                    filterActivitiesImpacts(proj.proj_reference,false);
                    refreshData();
                }
                else
                {
                    if (MessageBox.Show("Value already added, Do you need to update ?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                         
                        if (new cecbClasses.Data_Adapters.Func_FillMatrix().UpdateValuesLeopold(leoExtra.impt_reference, leoExtra.actv_reference, proj.proj_reference, leoExtra.mtrx_importance.ToString(), leoExtra.mtrx_magnitude.ToString()))
                        {
                            MessageBox.Show("Values updated!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            filterActivitiesImpacts(proj.proj_reference,false);
                            refreshData();
                        }
                        else
                        {

                            MessageBox.Show("System Error");
                        }
                    }
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("No value to confirm!", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


        private void saveSimpleValues()
        {

            SimpleMatrixValuesExtra simx = (SimpleMatrixValuesExtra)listBoxMembersValuesSim.SelectedItem;

            try
            {
                if (new cecbClasses.Data_Adapters.Func_FillMatrix().ConfirmDuplicatedValuesSimple(simx.impt_reference, simx.actv_reference, proj.proj_reference, simx.mtrx_simpleValue.ToString(), simx.mem_reference))
                {
                    MessageBox.Show("Value added to matrix!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                    filterActivitiesImpacts(proj.proj_reference, true);
                    refreshData();
                }
                else
                {
                    if (MessageBox.Show("Value already added, Do you need to update ?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {

                        if (new cecbClasses.Data_Adapters.Func_FillMatrix().UpdateValuesSimple(simx.impt_reference, simx.actv_reference, proj.proj_reference, simx.mtrx_simpleValue.ToString()))
                        {
                            MessageBox.Show("Values updated!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            filterActivitiesImpacts(proj.proj_reference, true);
                            
                            refreshData();
                        }
                        else
                        {

                            MessageBox.Show("System Error");
                        }
                    }
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("No value to confirm!", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (!isSimpleMatrix)
            {
                saveLeopoldValues();
            }
            else {
                saveSimpleValues();
            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
           // var itemContainer = listBoxProjectsLeo.ItemContainerGenerator.ContainerFromIndex(listBoxProjectsLeo.SelectedIndex) as ListBoxItem;
        }

        private void listBoxProjectsSim_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxProjectsSim.SelectedItems.Count > 0)
            {
                proj = (Projects)listBoxProjectsSim.SelectedItem;
                ProjectAutocompleteTextBox.Text = proj.proj_name;
                filterActivitiesImpacts(proj.proj_reference, true);
                isSimpleMatrix = true;
                refreshData();
            }
        }

        private void listBoxProjectsLeo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxProjectsLeo.SelectedItems.Count > 0)
            {
                proj = (Projects)listBoxProjectsLeo.SelectedItem;
                ProjectAutocompleteTextBox.Text = proj.proj_name;
                filterActivitiesImpacts(proj.proj_reference, false);
                isSimpleMatrix = false;
                refreshData();
                
            }
        }


        private void listBoxProjectsLeo_GotFocus(object sender, RoutedEventArgs e)
        {
            listBoxProjectsSim.SelectedIndex = -1;
            listBoxProjectsSim.Background = Brushes.Transparent;
            listBoxProjectsLeo.Background = Brushes.Green;

        }

        private void listBoxProjectsSim_GotFocus(object sender, RoutedEventArgs e)
        {
            listBoxProjectsLeo.SelectedIndex = -1;
            
             
            listBoxProjectsSim.Background = Brushes.Green;
            listBoxProjectsLeo.Background = Brushes.Transparent;
        }

    }
}
